package com.gw.service.impl;

import com.gw.context.BaseContext;
import com.gw.mapper.PostSAMapper;
import com.gw.mapper.UserMapper;
import com.gw.pojo.dto.PostSADTO;
import com.gw.pojo.entity.PostSA;
import com.gw.pojo.vo.PostSAVO;
import com.gw.properties.BucketProperties;
import com.gw.service.PostSAService;
import com.gw.utils.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class PostSAServiceImpl implements PostSAService {

    @Autowired
    private PostSAMapper postMapper;
    @Autowired
    private UserMapper   userMapper;
    @Autowired MinioUtil minioUtil;
    @Override
    public List<PostSA> getAllPosts() {
        return postMapper.getAllPosts();
    }

    @Override
    public PostSAVO addPost(PostSADTO addPostDTO) {
        String pictureUrl= addPostDTO.getPictureUrl();
        String attachmentUrl= addPostDTO.getAttachmentUrl();
        Long userId = BaseContext.getCurrentUserId();
        PostSA post = PostSA.builder()
                .title(addPostDTO.getTitle())
                .content(addPostDTO.getContent())
                .typeCode(addPostDTO.getTypeCode())
                .pictureUrl(pictureUrl)
                .attachmentUrl(attachmentUrl)
                .build();
//        BeanUtils.copyProperties(addPostDTO, post);
        // 构建返回的VO对象
        String userNickname = userMapper.getNicById(userId);
        PostSAVO postSAVO = PostSAVO.builder()
                .id(userId)
                .title(addPostDTO.getTitle())
                .content(addPostDTO.getContent())
                .attachmentUrl(attachmentUrl)
                .pictureUrl(pictureUrl)
                .authorName(userNickname)
                .createTime(post.getCreatedTime())
                .typeCode(addPostDTO.getTypeCode())
                .build();

        post.setAuthorId(userId);
        postMapper.addPost(post);
        return postSAVO;
    }


    @Override
    public List<PostSA> getLatestPosts(int limit) {
        return postMapper.getLatestPost(limit);
    }

    @Override
    public void deletePost(Long id) {
        postMapper.deletePostById(id);
        //TODO 删除的同时将其中的附件资源从minio中删除
//        PostSA postSA=postMapper.getById(id);
//        String pictureUrl=postSA.getPictureUrl();
//        String attachmentUrl = postSA.getAttachmentUrl();
//        minioUtil.
    }



    @Override
    public void updatePost(PostSADTO updatePostDTO) {
        String pictureUrl;
        String attachmentUrl;
//        MultipartFile picture=updatePostDTO.getAttachment();
//        MultipartFile attachment=updatePostDTO.getAttachment();
        String pictureUrl1=updatePostDTO.getPictureUrl();
        String attachmentUrl1=updatePostDTO.getAttachmentUrl();
        PostSA post = PostSA.builder()
                .title(updatePostDTO.getTitle())
                .content(updatePostDTO.getContent())
                .typeCode(updatePostDTO.getTypeCode())
                .build();
        Long userId = BaseContext.getCurrentUserId();

//        BeanUtils.copyProperties(addPostDTO, post);
        post.setAuthorId(userId);
        postMapper.addPost(post);
    }

}
